package com.cheng.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cheng.common.domain.R;
import com.cheng.system.model.UserRole;
import com.cheng.system.service.impl.UserRoleServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * <p>
 * 用户和角色关联表 前端控制器
 * </p>
 *
 * @author 起凡
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/user-role")
@Slf4j
public class UserRoleController {
    @Autowired
    UserRoleServiceImpl roleService;

    @GetMapping("update")
    public R<Boolean> update(Long userId, long[] roleIds) {
        log.info(Arrays.toString(roleIds));
        roleService.remove(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, userId));
        ArrayList<UserRole> userRoles = new ArrayList<>();
        for (long roleId : roleIds) {
            userRoles.add(new UserRole().setRoleId(roleId).setUserId(userId));
        }
        roleService.saveBatch(userRoles);
        return R.ok(true);
    }
}

